package com.eatme.dao;

import org.apache.wicket.model.LoadableDetachableModel;
import com.eatme.model.BasicList;

/**
 * Detachable, read-only Contact model. Ensures that memory used to load the
 * contact details is immediately freed rather than held in the session.
 * Typically used by <tt>List</tt>-type pages, where multiple elements are
 * loaded at a time.
 *
 * @author ivaynberg
 */

public class DetachableBasicListModel extends LoadableDetachableModel<BasicList> {
    /**
     * database identity of the contact
     */
    private final long id;

    /**
     * dao reference - must be a wicket-wrapped proxy, holding onto a reference
     * to the real dao will cause its serialization into session or a
     * not-serializable exception when the servlet container serializes the
     * session.
     */
    private final BasicListDao dao;

    /**
     * Constructor
     *
     * @param basicList
     * @param dao
     */
    public DetachableBasicListModel(BasicList basicList, BasicListDao dao) {
        super(basicList);
        this.id = basicList.getId();
        this.dao = dao;
    }

    /**
     * Loads the contact from the database
     *
     * @see wicket.model.LoadableDetachableModel#load()
     */
    @Override
    protected BasicList load() {
        return dao.load(id);
    }
}
